/*======================= COPYRIGHT NOTICE ==================================*]
[* Copyright (c) 2019-2020 Qualcomm Technologies, Inc.                       *]
[* All Rights Reserved.                                                      *]
[* Confidential and Proprietary - Qualcomm Technologies, Inc.                *]
[*===========================================================================*/

#ifndef _EXP_LUT_KALIMBA_H_
#define _EXP_LUT_KALIMBA_H_

#include "hal/hal_dm_sections.h"


#define EXP_N_LUT 513             /**<Total number of elements in LUT, 512 + 1 */
#define EXP_LUT_SCALING_FACTOR 4  /**<Scaling factor for elements in LUT */
#define EXP_LOG_2_N_LUT 9         /**<log_base_2(512) */
#define EXP_INPUT_OFFSET 0.5r     /**<Offset for input to limit range in -0.5 to 0.5 */
#define EXP_INDEX_OFFSET 256      /**<Offset to shift index range from (-256,255) to (0,511) */
#define EXP_SPC 0.001953125r      /**<Resolution for rounding adjustment, Range/Datapoints, (0 - (-1))/512 = 1/512 */
#define EXP_SPC_BY_2 EXP_SPC >> 1 /**<Half of resolution for rounding adjustment */
#define EXP_INDEX_SHIFT 22        /**<Shift for index */
#define EXP_INDEX_MASK 0x1FF      /**<Mask to get index */
#define EXP_DELTA_MASK 0x3FFFFF   /**<Mask to get last 22 bits of input */

/**
 * @brief Look up table for exponential function
 * @note
 *    - Range : [0, -16]
 *    - Precision : 10 decimal digits
 *    - No presumed scaling
 *    - (-1.0) at index 0 rounded to accomodate range of sat fract
 */

DMCONST sat fract exp_lut[EXP_N_LUT] = {
    0.9999999995r, 0.9692332344r, 0.9394130628r, 0.9105103612r, 0.8824969027r, 0.8553453274r, 0.8290291182r,
    0.8035225738r, 0.7788007832r, 0.7548396019r, 0.7316156290r, 0.7091061822r, 0.6872892790r, 0.6661436106r,
    0.6456485265r, 0.6257840097r, 0.6065306598r, 0.5878696730r, 0.5697828247r, 0.5522524500r, 0.5352614284r,
    0.5187931657r, 0.5028315778r, 0.4873610768r, 0.4723665528r, 0.4578333618r, 0.4437473100r, 0.4300946407r,
    0.4168620198r, 0.4040365238r, 0.3916056268r, 0.3795571881r, 0.3678794410r, 0.3565609804r, 0.3455907525r,
    0.3349580429r, 0.3246524674r, 0.3146639611r, 0.3049827688r, 0.2955994355r, 0.2865047967r, 0.2776899710r,
    0.2691463488r, 0.2608655863r, 0.2528395960r, 0.2450605393r, 0.2375208191r, 0.2302130717r, 0.2231301600r,
    0.2162651666r, 0.2096113870r, 0.2031623228r, 0.1969116754r, 0.1908533401r, 0.1849813997r, 0.1792901205r,
    0.1737739434r, 0.1684274813r, 0.1632455126r, 0.1582229761r, 0.1533549670r, 0.1486367304r, 0.1440636590r,
    0.1396312863r, 0.1353352834r, 0.1311714542r, 0.1271357331r, 0.1232241779r, 0.1194329681r, 0.1157584023r,
    0.1121968906r, 0.1087449552r, 0.1053992244r, 0.1021564314r, 0.0990134082r, 0.0959670860r, 0.0930144894r,
    0.0901527344r, 0.0873790262r, 0.0846906560r, 0.0820849985r, 0.0795595087r, 0.0771117201r, 0.0747392420r,
    0.0724397572r, 0.0702110198r, 0.0680508539r, 0.0659571495r, 0.0639278614r, 0.0619610078r, 0.0600546678r,
    0.0582069801r, 0.0564161395r, 0.0546803973r, 0.0529980585r, 0.0513674794r, 0.0497870683r, 0.0482552815r,
    0.0467706225r, 0.0453316416r, 0.0439369334r, 0.0425851364r, 0.0412749294r, 0.0400050334r, 0.0387742077r,
    0.0375812510r, 0.0364249973r, 0.0353043182r, 0.0342181185r, 0.0331653375r, 0.0321449474r, 0.0311559513r,
    0.0301973834r, 0.0292683076r, 0.0283678165r, 0.0274950303r, 0.0266490974r, 0.0258291909r, 0.0250345101r,
    0.0242642793r, 0.0235177460r, 0.0227941810r, 0.0220928779r, 0.0214131512r, 0.0207543378r, 0.0201157941r,
    0.0194968963r, 0.0188970398r, 0.0183156389r, 0.0177521259r, 0.0172059503r, 0.0166765791r, 0.0161634944r,
    0.0156661961r, 0.0151841980r, 0.0147170294r, 0.0142642339r, 0.0138253695r, 0.0134000075r, 0.0129877329r,
    0.0125881424r, 0.0122008459r, 0.0118254651r, 0.0114616337r, 0.0111089963r, 0.0107672089r, 0.0104359365r,
    0.0101148565r, 0.0098036551r, 0.0095020281r, 0.0092096818r, 0.0089263297r, 0.0086516952r, 0.0083855106r,
    0.0081275157r, 0.0078774584r, 0.0076350942r, 0.0074001872r, 0.0071725072r, 0.0069518322r, 0.0067379470r,
    0.0065306420r, 0.0063297153r, 0.0061349706r, 0.0059462176r, 0.0057632714r, 0.0055859541r, 0.0054140924r,
    0.0052475184r, 0.0050860695r, 0.0049295872r, 0.0047779200r, 0.0046309186r, 0.0044884402r, 0.0043503456r,
    0.0042164996r, 0.0040867715r, 0.0039610346r, 0.0038391664r, 0.0037210477r, 0.0036065630r, 0.0034956010r,
    0.0033880523r, 0.0032838131r, 0.0031827809r, 0.0030848570r, 0.0029899459r, 0.0028979550r, 0.0028087944r,
    0.0027223765r, 0.0026386180r, 0.0025574360r, 0.0024787523r, 0.0024024891r, 0.0023285723r, 0.0022569294r,
    0.0021874909r, 0.0021201889r, 0.0020549577r, 0.0019917334r, 0.0019304543r, 0.0018710601r, 0.0018134937r,
    0.0017576986r, 0.0017036200r, 0.0016512051r, 0.0016004029r, 0.0015511634r, 0.0015034392r, 0.0014571832r,
    0.0014123502r, 0.0013688970r, 0.0013267803r, 0.0012859595r, 0.0012463951r, 0.0012080474r, 0.0011708797r,
    0.0011348557r, 0.0010999395r, 0.0010660980r, 0.0010332977r, 0.0010015066r, 0.0009706933r, 0.0009408281r,
    0.0009118821r, 0.0008838265r, 0.0008566338r, 0.0008302778r, 0.0008047330r, 0.0007799738r, 0.0007559769r,
    0.0007327176r, 0.0007101744r, 0.0006883247r, 0.0006671469r, 0.0006466210r, 0.0006267265r, 0.0006074444r,
    0.0005887551r, 0.0005706414r, 0.0005530845r, 0.0005360679r, 0.0005195746r, 0.0005035889r, 0.0004880955r,
    0.0004730783r, 0.0004585232r, 0.0004444160r, 0.0004307427r, 0.0004174900r, 0.0004046452r, 0.0003921958r,
    0.0003801291r, 0.0003684335r, 0.0003570979r, 0.0003461111r, 0.0003354629r, 0.0003251415r, 0.0003151381r,
    0.0003054421r, 0.0002960446r, 0.0002869363r, 0.0002781083r, 0.0002695518r, 0.0002612583r, 0.0002532206r,
    0.0002454296r, 0.0002378784r, 0.0002305601r, 0.0002234662r, 0.0002165912r, 0.0002099271r, 0.0002034684r,
    0.0001972085r, 0.0001911409r, 0.0001852601r, 0.0001795604r, 0.0001740358r, 0.0001686811r, 0.0001634913r,
    0.0001584613r, 0.0001535858r, 0.0001488607r, 0.0001442805r, 0.0001398418r, 0.0001355391r, 0.0001313691r,
    0.0001273272r, 0.0001234100r, 0.0001196130r, 0.0001159329r, 0.0001123659r, 0.0001089089r, 0.0001055580r,
    0.0001023104r, 0.0000991626r, 0.0000961116r, 0.0000931546r, 0.0000902885r, 0.0000875108r, 0.0000848183r,
    0.0000822088r, 0.0000796793r, 0.0000772281r, 0.0000748518r, 0.0000725491r, 0.0000703167r, 0.0000681533r,
    0.0000660564r, 0.0000640242r, 0.0000620545r, 0.0000601453r, 0.0000582947r, 0.0000565010r, 0.0000547627r,
    0.0000530779r, 0.0000514449r, 0.0000498621r, 0.0000483282r, 0.0000468409r, 0.0000454001r, 0.0000440031r,
    0.0000426495r, 0.0000413372r, 0.0000400655r, 0.0000388324r, 0.0000376380r, 0.0000364799r, 0.0000353577r,
    0.0000342699r, 0.0000332152r, 0.0000321935r, 0.0000312030r, 0.0000302428r, 0.0000293124r, 0.0000284105r,
    0.0000275364r, 0.0000266894r, 0.0000258680r, 0.0000250721r, 0.0000243010r, 0.0000235531r, 0.0000228286r,
    0.0000221264r, 0.0000214456r, 0.0000207857r, 0.0000201459r, 0.0000195261r, 0.0000189254r, 0.0000183433r,
    0.0000177789r, 0.0000172318r, 0.0000167019r, 0.0000161878r, 0.0000156900r, 0.0000152071r, 0.0000147391r,
    0.0000142856r, 0.0000138460r, 0.0000134204r, 0.0000130073r, 0.0000126073r, 0.0000122194r, 0.0000118432r,
    0.0000114790r, 0.0000111256r, 0.0000107833r, 0.0000104518r, 0.0000101300r, 0.0000098185r, 0.0000095163r,
    0.0000092234r, 0.0000089398r, 0.0000086646r, 0.0000083982r, 0.0000081398r, 0.0000078892r, 0.0000076466r,
    0.0000074115r, 0.0000071833r, 0.0000069621r, 0.0000067479r, 0.0000065407r, 0.0000063390r, 0.0000061444r,
    0.0000059553r, 0.0000057719r, 0.0000055945r, 0.0000054222r, 0.0000052555r, 0.0000050939r, 0.0000049369r,
    0.0000047851r, 0.0000046380r, 0.0000044950r, 0.0000043567r, 0.0000042231r, 0.0000040927r, 0.0000039670r,
    0.0000038450r, 0.0000037267r, 0.0000036121r, 0.0000035008r, 0.0000033933r, 0.0000032890r, 0.0000031875r,
    0.0000030897r, 0.0000029947r, 0.0000029025r, 0.0000028131r, 0.0000027264r, 0.0000026426r, 0.0000025611r,
    0.0000024824r, 0.0000024061r, 0.0000023320r, 0.0000022603r, 0.0000021909r, 0.0000021234r, 0.0000020582r,
    0.0000019949r, 0.0000019334r, 0.0000018738r, 0.0000018161r, 0.0000017602r, 0.0000017062r, 0.0000016536r,
    0.0000016028r, 0.0000015534r, 0.0000015055r, 0.0000014594r, 0.0000014147r, 0.0000013709r, 0.0000013290r,
    0.0000012880r, 0.0000012484r, 0.0000012098r, 0.0000011725r, 0.0000011367r, 0.0000011018r, 0.0000010678r,
    0.0000010347r, 0.0000010030r, 0.0000009723r, 0.0000009420r, 0.0000009132r, 0.0000008852r, 0.0000008577r,
    0.0000008317r, 0.0000008061r, 0.0000007814r, 0.0000007572r, 0.0000007339r, 0.0000007111r, 0.0000006892r,
    0.0000006682r, 0.0000006477r, 0.0000006277r, 0.0000006082r, 0.0000005895r, 0.0000005714r, 0.0000005541r,
    0.0000005369r, 0.0000005201r, 0.0000005043r, 0.0000004889r, 0.0000004736r, 0.0000004591r, 0.0000004452r,
    0.0000004312r, 0.0000004182r, 0.0000004051r, 0.0000003930r, 0.0000003809r, 0.0000003688r, 0.0000003576r,
    0.0000003465r, 0.0000003357r, 0.0000003255r, 0.0000003157r, 0.0000003059r, 0.0000002966r, 0.0000002873r,
    0.0000002785r, 0.0000002701r, 0.0000002617r, 0.0000002538r, 0.0000002459r, 0.0000002384r, 0.0000002310r,
    0.0000002240r, 0.0000002170r, 0.0000002100r, 0.0000002040r, 0.0000001974r, 0.0000001914r, 0.0000001853r,
    0.0000001797r, 0.0000001742r, 0.0000001690r, 0.0000001639r, 0.0000001588r, 0.0000001537r, 0.0000001490r,
    0.0000001444r, 0.0000001402r, 0.0000001360r, 0.0000001318r, 0.0000001276r, 0.0000001234r, 0.0000001196r,
    0.0000001159r, 0.0000001126r};

/**
 * @brief Look up table for exponential function
 * @note
 *    - Range : [0, 1]
 *    - Precision : 10 decimal digits
 *    - No presumed scaling
 *    - (-1.0) at index 0 rounded to accomodate range of sat fract
 *  This table is generated as follows:
 *  import numpy as np
 *  x = np.linspace(0,1,512)
 *  y = np.exp(x)
 *  y_fxp = [int(i*(2**29)) for i in y] # y in Q3.29
 *  print(y_fxp)
 */

DMCONST int exp_lut_positive[EXP_N_LUT] = { 
 536870912, 537922568, 538976285, 540032066, 541089915, 542149836,
 543211833, 544275911, 545342073, 546410324, 547480667, 548553106,
 549627647, 550704292, 551783047, 552863914, 553946899, 555032005,
 556119237, 557208599, 558300094, 559393727, 560489503, 561587426,
 562687499, 563789726, 564894113, 566000664, 567109382, 568220271,
 569333337, 570448583, 571566014, 572685634, 573807446, 574931457,
 576057669, 577186087, 578316716, 579449559, 580584621, 581721907,
 582861421, 584003167, 585147149, 586293372, 587441841, 588592559,
 589745531, 590900762, 592058256, 593218017, 594380050, 595544359,
 596710949, 597879824, 599050989, 600224448, 601400206, 602578267,
 603758635, 604941316, 606126313, 607313631, 608503276, 609695251,
 610889560, 612086209, 613285202, 614486544, 615690239, 616896292,
 618104708, 619315490, 620528645, 621744175, 622962087, 624182385,
 625405073, 626630156, 627857638, 629087526, 630319822, 631554532,
 632791661, 634031214, 635273194, 636517608, 637764458, 639013752,
 640265492, 641519685, 642776334, 644035445, 645297022, 646561071,
 647827596, 649096601, 650368093, 651642075, 652918553, 654197531,
 655479014, 656763008, 658049517, 659338546, 660630100, 661924184,
 663220803, 664519962, 665821666, 667125919, 668432728, 669742096,
 671054029, 672368532, 673685610, 675005268, 676327511, 677652344,
 678979773, 680309801, 681642435, 682977679, 684315539, 685656020,
 686999126, 688344863, 689693237, 691044251, 692397913, 693754225,
 695113195, 696474827, 697839126, 699206097, 700575746, 701948078,
 703323099, 704700812, 706081225, 707464341, 708850167, 710238708,
 711629968, 713023954, 714420671, 715820123, 717222317, 718627257,
 720034950, 721445400, 722858612, 724274594, 725693348, 727114883,
 728539201, 729966310, 731396214, 732828919, 734264431, 735702754,
 737143895, 738587859, 740034652, 741484279, 742936745, 744392057,
 745850219, 747311237, 748775118, 750241866, 751711487, 753183987,
 754659372, 756137646, 757618816, 759102888, 760589867, 762079758,
 763572568, 765068302, 766566967, 768068566, 769573108, 771080596,
 772591038, 774104438, 775620802, 777140138, 778662449, 780187742,
 781716023, 783247298, 784781572, 786318852, 787859143, 789402451,
 790948783, 792498143, 794050539, 795605975, 797164459, 798725995,
 800290590, 801858249, 803428980, 805002788, 806579678, 808159657,
 809742731, 811328907, 812918189, 814510585, 816106100, 817704740,
 819306511, 820911421, 822519474, 824130677, 825745037, 827362558,
 828983248, 830607113, 832234159, 833864392, 835497818, 837134444,
 838774276, 840417320, 842063582, 843713070, 845365788, 847021744,
 848680944, 850343394, 852009100, 853678070, 855350308, 857025823,
 858704619, 860386704, 862072084, 863760765, 865452754, 867148058,
 868846682, 870548634, 872253920, 873962546, 875674519, 877389846,
 879108532, 880830586, 882556012, 884284819, 886017012, 887752598,
 889491584, 891233976, 892979782, 894729007, 896481659, 898237744,
 899997269, 901760240, 903526665, 905296550, 907069903, 908846728,
 910627035, 912410829, 914198117, 915988906, 917783203, 919581015,
 921382348, 923187210, 924995608, 926807547, 928623037, 930442082,
 932264691, 934090870, 935920626, 937753967, 939590899, 941431429,
 943275564, 945123312, 946974679, 948829673, 950688301, 952550569,
 954416485, 956286057, 958159290, 960036193, 961916773, 963801036,
 965688991, 967580643, 969476001, 971375072, 973277863, 975184381,
 977094634, 979008629, 980926373, 982847874, 984773138, 986702174,
 988634989, 990571589, 992511984, 994456179, 996404183, 998356002,
 1000311645, 1002271119, 1004234431, 1006201589, 1008172600, 1010147472,
 1012126213, 1014108830, 1016095330, 1018085722, 1020080013, 1022078210,
 1024080321, 1026086355, 1028096318, 1030110218, 1032128063, 1034149860,
 1036175618, 1038205345, 1040239047, 1042276733, 1044318411, 1046364088,
 1048413772, 1050467471, 1052525193, 1054586946, 1056652738, 1058722576,
 1060796468, 1062874424, 1064956449, 1067042553, 1069132744, 1071227029,
 1073325416, 1075427914, 1077534530, 1079645273, 1081760150, 1083879170,
 1086002341, 1088129671, 1090261168, 1092396841, 1094536697, 1096680745,
 1098828992, 1100981448, 1103138120, 1105299017, 1107464146, 1109633517,
 1111807137, 1113985016, 1116167160, 1118353579, 1120544280, 1122739273,
 1124938566, 1127142167, 1129350084, 1131562327, 1133778902, 1135999820,
 1138225089, 1140454716, 1142688711, 1144927082, 1147169837, 1149416986,
 1151668537, 1153924498, 1156184878, 1158449686, 1160718931, 1162992620,
 1165270764, 1167553370, 1169840447, 1172132005, 1174428051, 1176728595,
 1179033645, 1181343211, 1183657301, 1185975923, 1188299088, 1190626803,
 1192959078, 1195295922, 1197637343, 1199983351, 1202333954, 1204689162,
 1207048983, 1209413427, 1211782503, 1214156219, 1216534585, 1218917610,
 1221305303, 1223697673, 1226094729, 1228496481, 1230902938, 1233314108,
 1235730002, 1238150628, 1240575996, 1243006114, 1245440993, 1247880642,
 1250325069, 1252774285, 1255228299, 1257687119, 1260150756, 1262619219,
 1265092518, 1267570661, 1270053659, 1272541520, 1275034255, 1277531872,
 1280034383, 1282541795, 1285054119, 1287571364, 1290093540, 1292620657,
 1295152724, 1297689751, 1300231748, 1302778724, 1305330690, 1307887654,
 1310449627, 1313016619, 1315588638, 1318165697, 1320747803, 1323334967,
 1325927200, 1328524510, 1331126907, 1333734403, 1336347006, 1338964727,
 1341587576, 1344215563, 1346848697, 1349486989, 1352130450, 1354779089,
 1357432915, 1360091941, 1362756175, 1365425628, 1368100310, 1370780231,
 1373465402, 1376155833, 1378851534, 1381552516, 1384258788, 1386970362,
 1389687247, 1392409455, 1395136994, 1397869877, 1400608113, 1403351712,
 1406100687, 1408855045, 1411614800, 1414379960, 1417150537, 1419926541,
 1422707983, 1425494873, 1428287223, 1431085042, 1433888342, 1436697133,
 1439511426, 1442331232, 1445156562, 1447987426, 1450823835, 1453665801,
 1456513333, 1459366444 };

#endif /* _EXP_LUT_KALIMBA_H_ */
